﻿using Sasa.QualityTools.CodeAnalysis.Common;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml.Serialization;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Views
{
    public partial class CodeInspectionResultContent : UserControl
    {
        public CodeInspectionResultContent()
        {
            InitializeComponent();
        }


        public void SaveSetting()
        {
            var model = DataContext as CodeInspectionResultViewModel;
            model.Grid.Kind.DisplayIndex = kindColumn.DisplayIndex;
            model.Grid.CheckId.DisplayIndex = checkIdColumn.DisplayIndex;
            model.Grid.Category.DisplayIndex = categoryColumn.DisplayIndex;
            model.Grid.Project.DisplayIndex = projectColumn.DisplayIndex;
            model.Grid.File.DisplayIndex = fileColumn.DisplayIndex;
            model.Grid.Message.DisplayIndex = messageColumn.DisplayIndex;
            model.Grid.Save();
        }

        public void LoadSetting()
        {
            var model = DataContext as CodeInspectionResultViewModel;
            model.Grid.Load();
            kindColumn.DisplayIndex = model.Grid.Kind.DisplayIndex;
            checkIdColumn.DisplayIndex = model.Grid.CheckId.DisplayIndex;
            categoryColumn.DisplayIndex = model.Grid.Category.DisplayIndex;
            projectColumn.DisplayIndex = model.Grid.Project.DisplayIndex;
            fileColumn.DisplayIndex = model.Grid.File.DisplayIndex;
            messageColumn.DisplayIndex = model.Grid.Message.DisplayIndex;
        }

        private void OnProblemDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            Logger.Info("Began to focus the violation source code.");

            var problem = ((DataGridRow)sender).DataContext as ProblemViewModel;
            if (problem == null)
            {
                Logger.Warn("Failed to focus because the double-clicked content is not bound to any violation information.");
                return;
            }

            var model = (CodeInspectionResultViewModel)DataContext;
            model.ShowRule(problem);
            model.FocusRelatedCode(problem);

            e.Handled = true;
        }

        //private void ToggleHowToSearchPopupVisibility(object sender, RoutedEventArgs e)
        //{
        //    this.howToSearchPopup.IsOpen = !this.howToSearchPopup.IsOpen;
        //}

        private void OnResetColumns(object sender, RoutedEventArgs e)
        {
            var model = DataContext as CodeInspectionResultViewModel;
            model.Grid.Reset();
            kindColumn.DisplayIndex = model.Grid.Kind.DisplayIndex;
            checkIdColumn.DisplayIndex = model.Grid.CheckId.DisplayIndex;
            categoryColumn.DisplayIndex = model.Grid.Category.DisplayIndex;
            projectColumn.DisplayIndex = model.Grid.Project.DisplayIndex;
            fileColumn.DisplayIndex = model.Grid.File.DisplayIndex;
            messageColumn.DisplayIndex = model.Grid.Message.DisplayIndex;
        }

        private void OnSortRows(object sender, DataGridSortingEventArgs e)
        {
            var model = DataContext as CodeInspectionResultViewModel;

            if (e.Column.SortDirection == null || e.Column.SortDirection.Value == ListSortDirection.Descending)
            {
                e.Column.SortDirection = ListSortDirection.Ascending;
                model.Sort(e.Column.SortMemberPath);
            }
            else
            {
                e.Column.SortDirection = ListSortDirection.Descending;
                model.Reverse(e.Column.SortMemberPath);
            }

            e.Handled = true;
        }
    }
}
